﻿using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using System.Data.OleDb;


namespace Web.Pages.QuShui.DiXiaS.ChaXunGL
{
    public partial class ZhangWuCX_DBS : BasePage
    {
        #region 属性
        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtGridTab1
        {
            get
            {
                return ViewState["dtGridTab1"] as DataTable;
            }
            set
            {
                ViewState["dtGridTab1"] = value;
            }
        }

        /// <summary>
        /// 保存结果集2
        /// </summary>
        private DataTable dtGridTab2
        {
            get
            {
                return ViewState["dtGridTab2"] as DataTable;
            }
            set
            {
                ViewState["dtGridTab2"] = value;
            }
        }

       
        #endregion

        #region 变量
        /// <summary>
        /// 存储过程调用类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();
        /// <summary>
        /// 行业分类
        /// </summary>
        QS_JX_HANGYEFLXXBLL HYFL = new QS_JX_HANGYEFLXXBLL();
        /// <summary>
        /// 取水用途
        /// </summary>
        QS_JX_QUSHUIXXBLL QSYT = new QS_JX_QUSHUIXXBLL();
        /// <summary>
        /// 管理部门
        /// </summary>
        QS_JX_BUMENXXBLL GLBM = new QS_JX_BUMENXXBLL();
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {

            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
            }

            int index = ASPxPageControl1.ActiveTabIndex;


            switch (index)
            {
                case 0:
                    gvGridTab1.DataSource = this.dtGridTab1;
                    gvGridTab1.DataBind();
                    break;
                case 1:
                    this.gvGridTab2.DataSource = this.dtGridTab2;
                    this.gvGridTab2.DataBind();
                    break;
               
                default: break;

            }


            if (index == 1 || index == 2 || index == 3)
            {
                cmbZhangWuY2.ClientEnabled = true;
                cmbZhangWuM2.ClientEnabled = true;
            }

        }
        /// <summary>
        /// 查询信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXun_Click(object sender, EventArgs e)
        {
            int index = ASPxPageControl1.ActiveTabIndex;
            ActiveTabIndexChange(index);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            int wtype = BusinessTool.WType;
            if (wtype == 1)
            {
                //int index = ASPxPageControl1.TabIndex;
                //ASPxPageControl1.tab
                //ASPxPageControl1.TabPages.Remove(2);

            }
            gvGridTab1.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            gvGridTab2.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //gvGridtab7.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            PageTools.QSLoadCombox(cmbXingZhengQ, PageTools.eLoadType.行政区QS, null, "");
            //PageTools.QSLoadCombox(cmbQuShuiYT, PageTools.eLoadType.取水用途QS, null, "");
            //PageTools.QSLoadCombox(cmbHangYeLX, PageTools.eLoadType.行业分类QS, null, "");
            //PageTools.QSLoadCombox(cbmGuanLiBM, PageTools.eLoadType.管理部门QS, null, "");
            LoadQSYT();
            LoadHYFL();
            LoadGLBM();
            //账务年月
            int zwym = BusinessTool.GetSLYMorZWNY(2, wtype);
            int y_now = DateTime.Now.Year;
            //cmbZhangWuY2.SelectedIndex = 0;
            //cmbZhangWuM2.SelectedIndex = 0;
            cmbZhangWuY.Items.Add("全部", 0);
            for (int i = y_now - 20; i <= y_now; i++)
            {
                cmbZhangWuY.Items.Add(i.ToString(), i);
                cmbZhangWuY2.Items.Add(i.ToString(), i);
            }
            cmbZhangWuY.Value = zwym / 100;

            cmbZhangWuY2.Value = zwym / 100;
            for (int i = 1; i < 13; i++)
            {
                cmbZhangWuM.Items.Add(i.ToString(), i);
                cmbZhangWuM2.Items.Add(i.ToString(), i);
            }
            cmbZhangWuM.SelectedIndex = zwym % 100;
            cmbZhangWuM2.SelectedIndex = zwym % 100;

            //水量年月
            // txtShuiLiangYM.Text = BusinessTool.GetSLYMorZWNY(1, wtype).ToString();

        }
        /// <summary>
        /// 加载 取水用途
        /// </summary>
        private void LoadQSYT()
        {
            DataTable dt = QSYT.SelectQS_JX_QUSHUIXXesDynamic("I_JLZT=0", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbQuShuiYT.Items.Add(dt.Rows[i]["S_QUSHUIMC"].ToString(), dt.Rows[i]["S_QUSHUIBH"]);
            }
            cmbQuShuiYT.Items.Insert(0, new ListEditItem("", "-1"));
        }
        /// <summary>
        /// 加载行业分类
        /// </summary>
        private void LoadHYFL()
        {
            DataTable dt = HYFL.SelectQS_JX_HANGYEFLXXesDynamic("I_JLZT=0", "S_FENLEIBH");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbHangYeLX.Items.Add(dt.Rows[i]["S_FENLEIMC"].ToString(), dt.Rows[i]["S_FENLEIBH"]);
            }
            cmbHangYeLX.Items.Insert(0, new ListEditItem("", "0"));
        }

        /// <summary>
        /// 加载管理部门
        /// </summary>
        private void LoadGLBM()
        {
            DataTable dt = GLBM.SelectQS_JX_BUMENXXesDynamic("I_JLZT=0 and S_BUMENBH NOT IN(0,600)", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbmGuanLiBM.Items.Add(dt.Rows[i]["S_BUMENMC"].ToString(), dt.Rows[i]["S_BUMENBH"]);
            }
            cbmGuanLiBM.Items.Insert(0, new ListEditItem("", "0"));
        }
        /// <summary>
        /// 跟当前显示的tab也签选择查询的数据集
        /// </summary>
        /// <param name="index">当前选项卡的索引</param>
        private void ActiveTabIndexChange(int index)
        {
            int v_zwy = cmbZhangWuY.Text == "全部" ? 0 : Convert.ToInt32(cmbZhangWuY.Value);
            int v_zwm = cmbZhangWuM.Text == "全部" ? 0 : Convert.ToInt32(cmbZhangWuM.Value);
            int v_zwy2 = cmbZhangWuY2.Text == "全部" ? 0 : Convert.ToInt32(cmbZhangWuY2.Value);
            int v_zwm2 = cmbZhangWuM2.Text == "全部" ? 0 : Convert.ToInt32(cmbZhangWuM2.Value);
            int v_hnhw = cmbHuanNeiHW.Text != "" ? Convert.ToInt32(cmbHuanNeiHW.Value) : 0;
            string v_xzq = cmbXingZhengQ.Text != "" ? cmbXingZhengQ.Value.ToString() : null;
            string v_yhh = chkYongHuH.Checked && txtYongHuH.Text != "" ? txtYongHuH.Text : null;
            string v_yhm = chkYongHuM.Checked && txtYongHuM.Text != "" ? txtYongHuM.Text : null;
            string v_qsyt = rabQuShuiYT.Checked && cmbQuShuiYT.Text != "" ? cmbQuShuiYT.Value.ToString() : null;
            string v_hylx = rabHangYeLX.Checked && cmbHangYeLX.Text != "" ? cmbHangYeLX.Value.ToString() : null;
            string v_zgbm = rabGuanLiBM.Checked && cbmGuanLiBM.Text != "" ? cbmGuanLiBM.Value.ToString() : null;
            string v_ym = rabShuiLiangYM.Checked && cmbSLYM2Tab1.Text != "" && txtShuiLiangYM.Text != "" ? cmbSLYM2Tab1.Text + txtShuiLiangYM.Text : null;

            switch (index)
            {
                case 0://本期信息
                    LoadGridDataTab1(v_zwy, v_zwm, v_hnhw, v_xzq, v_yhh, v_yhm, v_qsyt, v_hylx, v_zgbm, v_ym);
                    break;
                case 1://本月到账
                    LoadGridDataTab2(v_zwy, v_zwm, v_zwy2, v_zwm2, v_hnhw, v_xzq, v_yhh, v_yhm, v_qsyt, v_hylx, v_zgbm, v_ym);
                    break;
               
                default: break;
            }
        }
        /// <summary>
        /// 加载查询数据  本期信息
        /// </summary>
        private void LoadGridDataTab1(int v_zwy, int v_zwm, int v_hnhw, string v_xzq, string v_yhh, string v_yhm, string v_qsyt, string v_hylx, string v_zgbm, string v_ym)
        {
            int wtype = 1;
            int jelx = Convert.ToInt32(cmbJinE1Tab1.Value);
            string str = null;
            string v_je = null;
            switch (jelx)
            {
                case 0: str = null;
                    break;
                case 1: str = " and W.KZJE";
                    break;
                case 2: str = " and W.SSJE1";
                    break;
                case 3: str = " and 0";
                    break;
                case 4: str = " and (W.KZJE-W.SSJE1)";
                    break;
                case 5: str = " and 0";
                    break;
                default: break;
            }

            if (rabJinETab1.Checked && str != "" && cmbJinE2Tab1.Text != "" && txtJinETab1.Text != "")
            {
                v_je = str + cmbJinE2Tab1.Text + txtJinETab1.Text;
            }


            string v_sj = rabShuiJiaTab1.Checked && cmbShuiJiaTab1.Text != "" && txtShuiJia.Text != "" ? cmbShuiJiaTab1.Text + txtShuiJia.Text : null;
            string v_qf = rabQianFeiTab1.Checked ? "1" : null;
            this.dtGridTab1 = _SpHelper.P_QSCX_ZWCX_BENQIXX(wtype, v_zwy, v_zwm, v_hnhw, v_xzq, v_yhh, v_yhm, v_qsyt, v_hylx, v_zgbm, v_ym, v_je, v_sj, v_qf).Tables[0];
            this.gvGridTab1.DataSource = this.dtGridTab1;
            this.gvGridTab1.DataBind();
            double kzje = 0, ssje = 0, tzje = 0, qfje = 0, dsje = 0;
            long sl = 0;
            if (this.dtGridTab1 != null && this.dtGridTab1.Rows.Count > 0)
            {
                for (int i = 0; i < dtGridTab1.Rows.Count; i++)
                {
                    kzje += dtGridTab1.Rows[i]["KZJE"].ToString() != "" ? Convert.ToDouble(dtGridTab1.Rows[i]["KZJE"]) : 0;
                    ssje += dtGridTab1.Rows[i]["SSJE"].ToString() != "" ? Convert.ToDouble(dtGridTab1.Rows[i]["SSJE"]) : 0;
                    tzje += dtGridTab1.Rows[i]["QFTZ"].ToString() != "" ? Convert.ToDouble(dtGridTab1.Rows[i]["QFTZ"]) : 0;
                    qfje += dtGridTab1.Rows[i]["QF"].ToString() != "" ? Convert.ToDouble(dtGridTab1.Rows[i]["QF"]) : 0;
                    dsje += dtGridTab1.Rows[i]["DS"].ToString() != "" ? Convert.ToDouble(dtGridTab1.Rows[i]["DS"]) : 0;
                    sl += dtGridTab1.Rows[i]["KZSL"].ToString() != "" ? Convert.ToInt32(dtGridTab1.Rows[i]["KZSL"]) : 0;
                }

            }
            lbZongYeStab1.Text = "共" + this.dtGridTab1.Rows.Count.ToString() + "条";
            txtKaiZhangJEtab1.Text = kzje.ToString();
            txtShiShouJEtab1.Text = ssje.ToString();
            txtHeXiaoJEtab1.Text = tzje.ToString();
            txtQianFeitab1.Text = qfje.ToString();
            txtDuoShoutab1.Text = dsje.ToString();
            txtKaiZhangSLtab1.Text = sl.ToString();
        }

        /// <summary>
        /// 加载tab2页签数据 本月到帐
        /// </summary>
        private void LoadGridDataTab2(int v_zwy, int v_zwm, int v_zwy2, int v_zwm2, int v_hnhw, string v_xzq, string v_yhh, string v_yhm, string v_qsyt, string v_hylx, string v_zgbm, string v_ym)
        {
            int wtype = 1;
            string v_je = rabJinETab2.Checked && cmbJinE2Tab2.Text != "" && txtJinETab2.Text != "" ? cmbJinE2Tab2.Text + txtJinETab2.Text : null;
            string v_dzfl = rbnDaoZhangFLtab2.Checked && cmbChaoBiaoBMtab2.Text != "" ? cmbChaoBiaoBMtab2.Value.ToString() : null;
            string v_sffs = rbnShouFeiFStab2.Checked && cmbShuiLiangYtab2.Text != "" ? cmbShuiLiangYtab2.Value.ToString() : null;
            string v_fylx = null;// rbnFeiYongLXtab2.Checked && cmbFeiYongLXtab2.Text != "" ? cmbFeiYongLXtab2.Value.ToString() : null;
            int v_sfdydz = rbnSFDYDZtab2.Checked ? 1 : 0;//1 显示开账年月=到账年月的用户
            this.dtGridTab2 = _SpHelper.P_QSCX_ZWCX_BENYUEDZ(wtype, v_zwy, v_zwm, v_zwy2, v_zwm2, v_hnhw, v_xzq, v_yhh, v_yhm, v_qsyt, v_hylx, v_zgbm, v_ym, v_je, v_dzfl, v_sffs, v_fylx, v_sfdydz).Tables[0];

            //if (wtype == 2)
            //{
            //    gvGridTab2.Columns.Clear();
            //    LoadColumnDate(gvGridTab2, "S_YONGHUH", "用户号", 80);
            //    LoadColumnDate(gvGridTab2, "S_YONGHUM", "用户名", 120);
            //    LoadColumnDate(gvGridTab2, "LX", "费用类型", 80);
            //    LoadColumnDate(gvGridTab2, "I_YM", "水量年月", 80);
            //    LoadColumnDate(gvGridTab2, "DZYM", "到账年月", 80);
            //    LoadColumnDate(gvGridTab2, "N_JINE", "到账金额", 100, "0.00");
            //    LoadColumnDate(gvGridTab2, "SFLX", "收费类型", 100);
            //    LoadColumnDate(gvGridTab2, "S_KAIHUH", "银行帐号", 120);
            //    LoadColumnDate(gvGridTab2, "S_KAIHUYH", "开户银行", 120);
            //    LoadColumnDate(gvGridTab2, "S_KAIHUHM", "开户户名", 120);
            //    LoadColumnDate(gvGridTab2, "USERNAME", "业务员", 80);
            //    LoadColumnDate(gvGridTab2, "USERNAME2", "操作员", 80);
            //    LoadColumnDate(gvGridTab2, "D_CAOZUORQ", "操作日期", 85, "yyyy-MM-dd");
            //    LoadColumnDate(gvGridTab2, "TSLX", "托收类型", 100);
            //    LoadColumnDate(gvGridTab2, "SL", "到账水量", 100);
            //    LoadColumnDate(gvGridTab2, "SZYF", "水资源费", 100, "0.00");
            //    LoadColumnDate(gvGridTab2, "S_ZHANGDANBH", "账单编号", 100);
            //    LoadColumnDate(gvGridTab2, "S_FAPIAOHM", "发票号码", 100);

            //}

            this.gvGridTab2.DataSource = this.dtGridTab2;
            this.gvGridTab2.DataBind();
            double zje = 0; long sl = 0; double znj = 0;
            if (dtGridTab2 != null && dtGridTab2.Rows.Count > 0)
            {
                for (int i = 0; i < dtGridTab2.Rows.Count; i++)
                {
                    zje += Convert.ToDouble(dtGridTab2.Rows[i]["N_JINE"]);
                    sl += Convert.ToInt32(dtGridTab2.Rows[i]["SL"]);
                    znj += Convert.ToDouble(dtGridTab2.Rows[i]["ZNJ"]);

                }

            }
            lbZongYeStab2.Text = "共" + this.dtGridTab2.Rows.Count.ToString() + "条";
            txtDaoZhangJEHZtab2.Text = zje.ToString();
            txtDaoZhangSLHZtab2.Text = sl.ToString();
            txtZNJtab2.Text = znj.ToString();
          
        }

     

        protected void ASPxCallbackControl_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {

        }

        #endregion

        protected void gvGrid2tab6_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {

        }

        private void LoadColumnDate(ASPxGridView gv, string fieldName, string caption, int width)
        {
            GridViewDataTextColumn c = new GridViewDataTextColumn();
            c.FieldName = fieldName;
            c.Caption = caption;
            c.Width = width;
            gv.Columns.Add(c);
        }

        private void LoadColumnDate(ASPxGridView gv, string fieldName, string caption, int width, string format)
        {
            GridViewDataTextColumn c = new GridViewDataTextColumn();
            c.FieldName = fieldName;
            c.Caption = caption;
            c.Width = width;
            c.PropertiesEdit.DisplayFormatString = format;
            gv.Columns.Add(c);
        }


        protected void btnExport_Click(object sender, EventArgs e)
        {
            int index = ASPxPageControl1.ActiveTabIndex;
            string sName = "";
            if (index == 0)
            {
                gvGridTab1.DataSource = this.dtGridTab1;
                gvGridTab1.DataBind();
                ASPxGridViewExporter1.GridViewID = "gvGridTab1";
                sName = "本期信息";
                gvGridTab1.Settings.ShowFooter = false;
            }
            else if (index == 1)
            {
                this.gvGridTab2.DataSource = this.dtGridTab2;
                this.gvGridTab2.DataBind();
                ASPxGridViewExporter1.GridViewID = "gvGridTab2";
                sName = "本月到账";
                gvGridTab2.Settings.ShowFooter = false;
            }
            

            ASPxGridViewExporter1.WriteXlsToResponse(sName);

        }
    }
}